package com.eop.managedBean;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;

import com.base.commons.SpringBeanHolder;
import com.eop.entity.Event;
import com.eop.entity.Minutes;
import com.eop.service.IEventService;
import com.eop.service.IMinutesService;

@ManagedBean(name = "eop_showMinutesBean", scope = ManagedBeanScope.REQUEST)
public class ShowMinutesBean {
	private IMinutesService minutersServce = SpringBeanHolder
			.getBean("minutesService");
	private IEventService eventService = SpringBeanHolder
			.getBean("eventService");

	@SaveState
	@Bind(id = "minuteName")
	private String minuteName = "";

	@Accessible
	@SaveState
	private String windowTitle;

	@Accessible
	@SaveState
	private String eventId = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("eventId");

	@BeforeRender
	public void beforeRender(boolean isPostBack) {
		if (!isPostBack) {
			minuteName = this.getMinuteName();
			if (!"".equals(eventId) && eventId != null) {
				Event event = eventService.getEventById(eventId);
				windowTitle = "会议记录：     " + event.getEventName();
			}
		}
	}

	private String getMinuteName() {
		StringBuilder str = new StringBuilder();

		if (!"".equals(eventId) && eventId != null) {

			List<Minutes> minutes = minutersServce.getMinutesByEvent(eventId);

			if (minutes != null && minutes.size() > 0) {
				// str.append("<div style=\"overflow-y:scroll;height:200px;\">");
				str.append("<ul>");
				for (int i = 0; i < minutes.size(); i++) {
					Minutes mt = minutes.get(i);
					String minutesContent = mt.getMinutes().replaceAll(
							"<(/?\\S+)\\s*?[^<]*?(/?)>", "<$1$2>");

					SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat
							.getInstance();
					formatter.applyPattern("yyyy-MM-dd HH:mm:ss");
					str
							.append("<li style=\"line-height:400%\">");
					str.append("<font color=\"#000099\" size=\"7\">");
					str.append(" [" + formatter.format(mt.getMinutesDate())
							+ "] ");
					str.append(minutesContent + ";</font>");
					str.append("</li>");
				}
				str.append("</ul>");
			}
		}
		return str.toString();
	}
}
